package com.nuit.cxl.aop.bilibili.aop;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class MyAdvice {


    //记得这里不是声明一个函数，是一个完整的函数，有大括号的，函数名字任意
    @Pointcut("execution(* com..*.*Dao.*(..))")
    public void pointCut(){}


    @After("pointCut()")
    public void after(){
        System.out.println("After：" + System.currentTimeMillis());
    }

    //before和after 因为不能传入JoinPoint的实现类ProceedingJoinPoint
    //所以并不能在里面修改参数，只能读取出来
    @Before("pointCut()")
    public void before(JoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        System.out.println(Arrays.toString(args));
        System.out.println("Before：" + System.currentTimeMillis());
    }

    //ProceedingJoinPoint可以把修改后的参数传进去，实现修改的增强
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Around before：" + System.currentTimeMillis());
        Object[] args = joinPoint.getArgs();
        args[0] = 100;
        Object ret = joinPoint.proceed(args);
        System.out.println("Around after：" + System.currentTimeMillis());
        return ret;
    }

}
